package jp.co.dailyReport.project.sql;

import java.util.Calendar;
import java.sql.Date;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import org.apache.log4j.Logger;
import jp.co.dailyReport.Executer;
import jp.co.dailyReport.project.Project;
import jp.co.dailyReport.Utilities;

/**
 * プロジェクト情報を更新するクラスです。
 */
public class ProjectUpdateExecuter implements Executer {

	private static Logger log = Logger.getLogger(ProjectUpdateExecuter.class);
	private Project project;

	public ProjectUpdateExecuter(Project project) {
		this.project = project;
	}

	/**
	 * プロジェクトIDを基に、プロジェクト情報を更新します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return null
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		String sql = "update project set customerId=?,name=?,periodStart=?,periodEnd=?, pLStaffId=?,outline=?,pjCustomerStaff=?,statusFlg=?,upDay=? where projectId=?";
		log.debug("sql: " + sql);
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, project.getCustomerId());
			pstmt.setString(2, Utilities.convUniqueCharacter(project.getName()));
			pstmt.setDate(3, new Date(project.getPeriodStart().getTime().getTime()));
			pstmt.setDate(4, new Date(project.getPeriodEnd().getTime().getTime()));
			pstmt.setInt(5, project.getPLStaffId());
			pstmt.setString(6, Utilities.convUniqueCharacter(project.getOutline()));
			pstmt.setString(7, Utilities.convUniqueCharacter(project.getPjCustomerStaff()));
			pstmt.setInt(8, Utilities.booleanToDecimal(project.isStatusFlg()));
			Calendar cal = Calendar.getInstance();
			pstmt.setDate(9, new Date(cal.getTime().getTime()));
			pstmt.setInt(10, project.getProjectId());
			pstmt.execute();
		} finally {
			if(pstmt != null)
			 pstmt.close();
		}
		return null;
	}

}